package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.CouponLead;
import cn.stylefeng.guns.sys.modular.tao.mapper.CouponLeadMapper;
import cn.stylefeng.guns.sys.modular.tao.model.params.CouponLeadParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.CouponLeadResult;
import  cn.stylefeng.guns.sys.modular.tao.service.CouponLeadService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 优惠券领取记录表 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-21
 */
@Service
public class CouponLeadServiceImpl extends ServiceImpl<CouponLeadMapper, CouponLead> implements CouponLeadService {

    @Resource
    private CouponLeadMapper couponLeadMapper;

    @Override
    public void add(CouponLeadParam param){
        CouponLead entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CouponLeadParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CouponLeadParam param){
        CouponLead oldEntity = getOldEntity(param);
        CouponLead newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CouponLeadResult findBySpec(CouponLeadParam param){
        return null;
    }

    @Override
    public List<CouponLeadResult> findListBySpec(CouponLeadParam param){
        return couponLeadMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CouponLeadParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void deleteCoupon(CouponLeadParam param) {
        couponLeadMapper.deleteCoupon(param);
    }

    @Override
    public void updateCoupon(CouponLeadParam param) {
        couponLeadMapper.updateCoupon(param);
    }

    @Override
    public void addCoupon(CouponLeadParam param) {
        couponLeadMapper.addCoupon(param);
    }

    private Serializable getKey(CouponLeadParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CouponLead getOldEntity(CouponLeadParam param) {
        return this.getById(getKey(param));
    }

    private CouponLead getEntity(CouponLeadParam param) {
        CouponLead entity = new CouponLead();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
